package com.jvm123.blog.interceptor

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @author yawn http://jvm123.com
 *     2019/10/18 16:29
 */
@Component
class AuditInterceptor extends HandlerInterceptorAdapter {

    Logger logger = LoggerFactory.getLogger(AuditInterceptor.class)

    @Override
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI()
        StringBuffer sb = new StringBuffer()
        for (String headerName : request.headerNames) {
            String header = request.getHeader(headerName)
            sb.append(headerName).append("=").append(header).append(" ")
        }
        logger.info("uri: {} , headers: {}", uri, new String(sb))
        true
    }

}
